package ds.tree;

public class LevelOfNodeInBinaryTree {
	private static class TreeNode{
		int value;
		TreeNode left = null;
		TreeNode right = null;
		
		TreeNode(int val){
			this.value = val;
		}
		
		public String toString(){
			return String.valueOf(value);
		}
	}

	public int getLevel(TreeNode node, TreeNode key,int level){
		if(key == null){
			return -1;
		}
		if(node == null ){
			return 0;
		}
		if(node.value == key.value){
			return level;
		}
		level = getLevel(node.left,key,level+1);
		if(level >	0 ){
			return level;
		}
		level = getLevel(node.right,key,level+1);		
		return level;
	}
	private static TreeNode tree(){
		TreeNode A = new TreeNode(1);
		TreeNode B = new TreeNode(2);
		TreeNode C = new TreeNode(3);
		TreeNode D = new TreeNode(4);
		TreeNode E = new TreeNode(5);
		TreeNode F = new TreeNode(6);
		TreeNode G = new TreeNode(7);
		
		A.left = B;
		A.right = C;
		B.left = D;
		B.right = E;
		C.left = F;
		C.right = G;
		
		return A;
	}

	public static void main(String[] args) {
		LevelOfNodeInBinaryTree lvlOfNode = new LevelOfNodeInBinaryTree();
		System.out.println(lvlOfNode.getLevel(tree(), new TreeNode(1),0));
	}

}
